#include "libraries.h"
#include "SDL_image.h"

using namespace std;

SDL_Surface* imgLoad(SDL_Surface*& surf, string img)
{
    SDL_Surface* tempSurf = IMG_Load( img.c_str() );
    if( tempSurf != NULL)
    {
        surf = SDL_DisplayFormat( tempSurf );
        SDL_FreeSurface( tempSurf );
    }
    return surf;
}

SDL_Surface* imgLoadAlpha(SDL_Surface*& surf, string img)
{
    SDL_Surface* tempSurf = IMG_Load( img.c_str() );
    if( tempSurf != NULL)
    {
        surf = SDL_DisplayFormatAlpha( tempSurf );
        SDL_FreeSurface( tempSurf );
    }
    return surf;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* dest, SDL_Rect* clip)
{
    SDL_Rect offset;
    offset.x = x;
    offset.y = y;
    SDL_BlitSurface( source, clip, dest, &offset );
}

TextElement::TextElement()
{
    font     = NULL;
    message  = NULL;
    color.r  = 255; /* Defaults to 0 */
    color.g  = 255; /* Defaults to 0 */
    color.b  = 255; /* Defaults to 0 */
    red = blue = green = 255; /* Initialize colors as white */
    x = y    = 0; /* Initialize coordinates at (0, 0) */
    size = 12; /* 12 is default size */
}

TextElement::TextElement(string text, int coordX, int coordY, int r, int g, int b, int siz, string pos, string fontStr)
{
    /* NULL out before assignment incase something goes horribly, horribly wrong */
    font     = NULL;
    message  = NULL;
    str      = text;
    x        = coordX;
    y        = coordY;
    red      = r;
    green    = g;
    blue     = b;
    size     = siz;
    position = pos;
    color.r  = red;
    color.g  = green;
    color.b  = blue;
    if(fontStr != "freesansbold.ttf" || size != 12)
    {
        fontStr = TTF_FILE_LOCATION + fontStr;
        font    = TTF_OpenFont(fontStr.c_str(), size);
    }
    fontStr = TTF_FILE_LOCATION + "freesansbold.ttf"; /* Default ttf file */
    if(!font) font = TTF_OpenFont(fontStr.c_str(), size); /* If font failed to load or default, go to defaults */
    message = TTF_RenderText_Solid( font, str.c_str(), color);
}

TextElement::~TextElement()
{
    //if(font) TTF_CloseFont(font);
}

void TextElement::draw(SDL_Surface*& screen) /* Draw self to screen */
{
    apply_surface(x, y, message, screen);
}

double pointDistance(SDL_Rect a, SDL_Rect b)
{
	/* This function will calculate the X and Y coordinates of the center of the 2 SDL_Rect
	 * Then this function will calculate the distance between the two centers using the distance
	 * formula: distance = (x^2 +y^2)^.5
	 */
	int AposX = a.x + (a.w)/2;
	int AposY = a.y + (a.h)/2;
	int BposX = b.x + (b.w)/2;
	int BposY = b.y + (b.h)/2;
	int PosX = AposX - BposX;
	int PosY = AposY - BposY;

	return sqrt(PosX * PosX + PosY * PosY);
}

/* collideCircle works best for square or "circular" images.  The function takes the largest
   side of both rectangles and checks to see if there is a collision between the circles made
   by those sides.
*/
bool collideCircle(SDL_Rect a, SDL_Rect b)
{
    double biggestRadiusA=1.0;
    double biggestRadiusB=1.0;

    if(a.w > a.h) biggestRadiusA = a.w/2.0;
    else biggestRadiusA = a.h/2.0;
    if(b.w > b.h) biggestRadiusB = b.w/2.0;
    else biggestRadiusB = b.h/2.0;

    if(pointDistance(a, b) < biggestRadiusA + biggestRadiusB) return true;
    else return false;
}

bool collideRect(SDL_Rect a, SDL_Rect b)
{
		//The sides of the rectangles
		int leftA,   leftB;
		int rightA,  rightB;
		int topA,    topB;
		int bottomA, bottomB;

		//Calculate the sides of rect A
		leftA		= a.x;
		rightA		= a.x + a.w;
		topA		= a.y;
		bottomA		= a.y + a.h;

		//Calculate the sides of rect box
		leftB		= b.x;
		rightB		= b.x + b.w;
		topB		= b.y;
		bottomB		= b.y + b.h;

		if     (bottomA <= topB)    return false;
		else if(topA    >= bottomB) return false;
		else if(leftA   >= rightB)   return false;
		else if(rightA  <= leftB)  return false;
		else                        return true;
}
